//
//  VideoRecordingController.m
//  VideoExam
//
//  Created by apple on 2020/4/11.
//

#import "VideoRecordingController.h"
#import "ZSVideoRecordingView.h"
#import "VideoRecordControlView.h"
#import "HWVideoVC.h"
#import <MediaPlayer/MediaPlayer.h>
@interface VideoRecordingController ()
@property(nonatomic,strong)VideoRecordControlView * controlView;//视屏录制控制层
@property(nonatomic,strong)HWVideoVC * recordController ;
@end

@implementation VideoRecordingController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}
- (void)initSubviews {
    [super initSubviews];
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];//app 使用中不息屏设置
    self.view.backgroundColor=[UIColor blackColor];
    self.recordController = [[HWVideoVC alloc] init];
    [self addChildViewController:self.recordController];
    [self.view addSubview:self.recordController.view];
    
    self.controlView = [[VideoRecordControlView alloc] initWithFrame:CGRectZero];
    self.controlView.mask_image_url=self.mask_image_url;//遮罩imageurl
    [self.view addSubview:self.controlView];
    [self.controlView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.left.right.mas_equalTo(0);
       make.height.mas_equalTo(self.view);
    }];
 
    @weakify(self);
    [self.controlView setStopRecord_block:^{
           @strongify(self);
           [self.recordController stopVideoRecoding];
       }];
    [self.controlView setStateBlock:^(NSString * _Nonnull stateStr, NSString * _Nonnull video_length) {
        @strongify(self);
          if ([stateStr isEqualToString:@"开始"]) {
               [self.recordController startRecordVideo];
          } else {
              [self.recordController finishBtnOnClick];//结束录制
               [self dismissController];
                  if (self.getVideoBlock) {
                  self.getVideoBlock(self.recordController.video_name,video_length);//回传name 和视频时长
              }
          }
    }];
   
    
//    [self.controlView setStateBlock:^(NSString * _Nonnull stateStr) {
//        @strongify(self);
//        if ([stateStr isEqualToString:@"开始"]) {
//             [self.recordController startRecordVideo];
//            NSLog(@"fggg");
//        } else {
//            [self.recordController finishBtnOnClick];//结束录制
//           [self dismissController];
//                if (self.getVideoBlock) {
//                self.getVideoBlock(self.recordController.video_name);//回传url
//            }
//        }
//    }];
}
- (void)dismissController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIButton * btn = [UIFactory create_btnWithTitle:@"开始" imageName:nil backgroundColor:nil TypefaceColor:[UIColor blackColor] fontSize:12 target:self action:@selector(kaishi)];
//    [self.view addSubview:btn];
//    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(40);
//        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
//        make.size.mas_equalTo(CGSizeMake(88, 88));
//    }];
//    
//    UIButton * btn2 = [UIFactory create_btnWithTitle:@"结束" imageName:nil backgroundColor:nil TypefaceColor:[UIColor blackColor] fontSize:12 target:self action:@selector(jieshu)];
//    [self.view addSubview:btn2];
//    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(btn.mas_right).offset(5);
//        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
//        make.size.mas_equalTo(CGSizeMake(88, 88));
//    }];
//    
//    UIButton * btn3 = [UIFactory create_btnWithTitle:@"保存" imageName:nil backgroundColor:nil TypefaceColor:[UIColor blackColor] fontSize:12 target:self action:@selector(baocun)];
//    [self.view addSubview:btn3];
//    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(btn2.mas_right).offset(5);
//        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
//        make.size.mas_equalTo(CGSizeMake(88, 88));
//    }];
    
}


- (void)baocun {
    NSString *videoPath=self.recordController.video_name;
//  videoPath是你视频文件的路径，我这里是加载工程中的
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error) {
            NSLog(@"Save video fail:%@",error);
        } else {
            NSLog(@"Save video succeed.成功");
        }
     }];
}
    


@end
